﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;
using GSQ.BusinessLogic;

namespace UIapp.Busquedas
{
    public partial class frmCatalogos : Plantillas.frmFindPlantilla
    {
        public frmCatalogos(Comun.Catalogos pCatalogo)
        {
            InitializeComponent();
            this.entityCollection = new List<object>();
            this.catalogo = pCatalogo;
            #region Iniciar form
            this.Text = string.Format("Buscar {0}", this.catalogo);
            switch (this.catalogo)
            {
                case Comun.Catalogos.Bancos:
                    #region
                    this.radGridView.Columns[1].FieldName = "BANCO";
                    break;
                    #endregion

                case Comun.Catalogos.Categorias:
                    #region
                    this.radGridView.Columns[1].FieldName = "CATEGORIA";
                    break;
                    #endregion

                case Comun.Catalogos.Ciudades:
                    #region
                    this.radGridView.Columns[1].FieldName = "CIUDAD";
                    break;
                    #endregion

                case Comun.Catalogos.Marcas:
                    #region
                    this.radGridView.Columns[1].FieldName = "MARCA";
                    break;
                    #endregion

                case Comun.Catalogos.Medidas:
                    #region
                    this.radGridView.Columns[1].FieldName = "MEDIDA";
                    break;
                    #endregion

                case Comun.Catalogos.Monedas:
                    #region
                    this.radGridView.Columns[1].FieldName = "MONEDA";
                    break;
                    #endregion

                case Comun.Catalogos.Descripciones:
                    #region
                    this.radGridView.Columns[1].FieldName = "DESCRIPCION";
                    break;
                    #endregion

                case Comun.Catalogos.Departamentos:
                    #region
                    this.radGridView.Columns[1].FieldName = "DEPARTAMENTO";
                    break;
                    #endregion

                case Comun.Catalogos.Modelos:
                    #region
                    this.radGridView.Columns[1].FieldName = "MODELO";
                    break;
                    #endregion

                case Comun.Catalogos.Versiones:
                    #region
                    this.radGridView.Columns[1].FieldName = "VERSION";
                    break;
                    #endregion
                default:
                    throw new Exception("No se ha implementado metodo para este catalogo");
            }

            #endregion
        }

        #region Variables
        Comun.Catalogos catalogo;
        List<object> entityCollection;
        #endregion

        #region Metodos auxiliares
        public void Listar()
        {
            switch (this.catalogo)
            {
                case Comun.Catalogos.Bancos:
                    #region
                    if (this.txtBuscar.Text.Trim() == string.Empty)
                    {
                        this.entityCollection = new BancosBL().GetAll().Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    else
                    {
                        this.entityCollection = new BancosBL().GetByDescripcion(this.txtBuscar.Text.Trim()).Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    break;
                    #endregion

                case Comun.Catalogos.Categorias:
                    #region
                    if (this.txtBuscar.Text.Trim() == string.Empty)
                    {
                        this.entityCollection = new CategoriasBL().GetAll().Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    else
                    {
                        this.entityCollection = new CategoriasBL().GetByDescripcion(this.txtBuscar.Text.Trim()).Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    break;
                    #endregion

                case Comun.Catalogos.Ciudades:
                    #region
                    if (this.txtBuscar.Text.Trim() == string.Empty)
                    {
                        this.entityCollection = new CiudadesBL().GetAll().Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    else
                    {
                        this.entityCollection = new CiudadesBL().GetByDescripcion(this.txtBuscar.Text.Trim()).Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    break;
                    #endregion

                case Comun.Catalogos.Marcas:
                    #region
                    if (this.txtBuscar.Text.Trim() == string.Empty)
                    {
                        this.entityCollection = new MarcasBL().GetAll().Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    else
                    {
                        this.entityCollection = new MarcasBL().GetByDescripcion(this.txtBuscar.Text.Trim()).Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    break;
                    #endregion

                case Comun.Catalogos.Medidas:
                    #region
                    if (this.txtBuscar.Text.Trim() == string.Empty)
                    {
                        this.entityCollection = new MedidasBL().GetAll().Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    else
                    {
                        this.entityCollection = new MedidasBL().GetByDescripcion(this.txtBuscar.Text.Trim()).Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    break;
                    #endregion

                case Comun.Catalogos.Monedas:
                    #region
                    if (this.txtBuscar.Text.Trim() == string.Empty)
                    {
                        this.entityCollection = new MonedasBL().GetAll().Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    else
                    {
                        this.entityCollection = new MonedasBL().GetByDescripcion(this.txtBuscar.Text.Trim()).Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    break;
                    #endregion

                case Comun.Catalogos.Descripciones:
                    #region
                    if (this.txtBuscar.Text.Trim() == string.Empty)
                    {
                        this.entityCollection = new DescripcionesBL().GetAll().Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    else
                    {
                        this.entityCollection = new DescripcionesBL().GetByDescripcion(this.txtBuscar.Text.Trim()).Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    break;
                    #endregion

                case Comun.Catalogos.Departamentos:
                    #region
                    if (this.txtBuscar.Text.Trim() == string.Empty)
                    {
                        this.entityCollection = new DepartamentosBL().GetAll().Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    else
                    {
                        this.entityCollection = new DepartamentosBL().GetByDescripcion(this.txtBuscar.Text.Trim()).Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    break;
                    #endregion

                case Comun.Catalogos.Modelos:
                    #region
                    if (this.txtBuscar.Text.Trim() == string.Empty)
                    {
                        this.entityCollection = new ModelosBL().GetAll().Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    else
                    {
                        this.entityCollection = new ModelosBL().GetByDescripcion(this.txtBuscar.Text.Trim()).Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    break;
                    #endregion

                case Comun.Catalogos.Versiones:
                    #region
                    if (this.txtBuscar.Text.Trim() == string.Empty)
                    {
                        this.entityCollection = new VersionesBL().GetAll().Where(b => b.ACTIVO == true).ToList<object>();
                    }
                    else
                    {
                        {
                            this.entityCollection = new VersionesBL().GetByDescripcion(this.txtBuscar.Text.Trim()).Where(b => b.ACTIVO == true).ToList<object>();
                        }
                    }
                    break;
                    #endregion
                default:
                    throw new Exception("No se ha implementado metodo para este catalogo");
            }

        }
        #endregion

        #region Tratamiento de eventos
        private void txtBuscar_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                e.Handled = true;
                this.backgroundWorker.RunWorkerAsync();
                Comun.ShowLoadingFRM();
            }
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Listar();
        }

        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Comun.CloseLoadingFRM();
            if (e.Error != null)
                Comun.ShowMessage(e.Error);
            else
                this.bindingSource.DataSource = this.entityCollection.ToList();
        }

        private void btnAceptar_Click(object sender, EventArgs e)
        {
            if (this.entityCollection.Count > 0)
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }
        #endregion
    }
}
